<template>
  <Top-Page>
    <cu-custom bgColor="bg-gradual-white-3" :isBack="true">
      <template #backText></template>
      <template #content>{{ type === "group" ? "群聊列表" : "服务团队" }}</template>
      <template #right>
        <view class="f-20 ml-10 mr-10" @tap="search()">
          <text class="cuIcon-search"></text>
        </view>
      </template>
    </cu-custom>
    <view class="cu-list menu-avatar no-padding">
      <template v-for="(items, sub) in list" :key="sub">
        <view v-if="!items.is_hide" class="cu-item" @tap="openDetails(items)">
          <view class="cu-avatar lg radius mr-15" :style="[{ backgroundImage: 'url(' + items.avatar + ')' }]"></view>
          <view class="content">
            <view class="c-333">{{ items.displayName }}</view>
          </view>
        </view>
      </template>
      <Empty v-if="!list.length" noDatatext="暂无信息" textcolor="#999"></Empty>
    </view>
  </Top-Page>
</template>

<script>
import { storeToRefs } from "pinia";
import { useMsgStore } from "@/store/message";
import pinia from "@/store/index";
const msgStore = useMsgStore(pinia);
const { contacts } = storeToRefs(msgStore);
/**
 * 初始的引导页
 */
export default {
  name: "group",
  data() {
    return {
      list: [],
      type: "",
    };
  },
  onLoad(params) {
    this.type = params.type;
  },
  created() {},
  mounted() {
    this.initContacts(this.msgs);
  },
  methods: {
    initContacts(arr) {
      const allContacts = uni.getStorageSync("allContacts");
      const contacts = allContacts.filter((item) => {
        if (this.type === "group") {
          return item.is_group == 1;
        } else {
          return item.identity === 1;
        }
      });
      // 将联系人进行排序
      const sorted = contacts.sort((a, b) => {
        if (a.index === "#") {
          return 1;
        }
        if (b.index === "#") {
          return -1;
        }
        return a.index.localeCompare(b.index, "zh");
      });
      this.list = sorted;
    },
    // 打开聊天
    openDetails(items) {
      uni.navigateTo({
        url: "/pages/message/chat?id=" + items.id,
      });
    },
    search() {
      const type = this.type === "group" ? 3 : 4;
      uni.navigateTo({
        url: `/pages/index/search?type=${type}`,
      });
    },
  },
};
</script>

<style scoped></style>
